package cn.iocoder.yudao.module.biz.service.customer;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.customer.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.CustomerArchiveDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.customer.CustomerArchiveMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 客户档案 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CustomerArchiveServiceImpl implements CustomerArchiveService {

    @Resource
    private CustomerArchiveMapper customerArchiveMapper;

    @Override
    public Long createBizCustomerArchive(CustomerArchiveSaveReqVO createReqVO) {
        // 插入
        CustomerArchiveDO bizCustomerArchive = BeanUtils.toBean(createReqVO, CustomerArchiveDO.class);
        customerArchiveMapper.insert(bizCustomerArchive);

        // 返回
        return bizCustomerArchive.getId();
    }

    @Override
    public void updateBizCustomerArchive(CustomerArchiveSaveReqVO updateReqVO) {
        // 校验存在
        validateBizCustomerArchiveExists(updateReqVO.getId());
        // 更新
        CustomerArchiveDO updateObj = BeanUtils.toBean(updateReqVO, CustomerArchiveDO.class);
        customerArchiveMapper.updateById(updateObj);
    }

    @Override
    public void deleteBizCustomerArchive(Long id) {
        // 校验存在
        validateBizCustomerArchiveExists(id);
        // 删除
        customerArchiveMapper.deleteById(id);
    }

    @Override
        public void deleteBizCustomerArchiveListByIds(List<Long> ids) {
        // 删除
        customerArchiveMapper.deleteByIds(ids);
        }


    private void validateBizCustomerArchiveExists(Long id) {
        if (customerArchiveMapper.selectById(id) == null) {
            throw exception(BIZ_CUSTOMER_ARCHIVE_NOT_EXISTS);
        }
    }

    @Override
    public CustomerArchiveDO getBizCustomerArchive(Long id) {
        return customerArchiveMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerArchiveDO> getBizCustomerArchivePage(CustomerArchivePageReqVO pageReqVO) {
        return customerArchiveMapper.selectPage(pageReqVO);
    }

    @Override
    public void changeServiceStatus(CustomerArchiveSaveReqVO updateReqVO) {
        // 校验存在
        validateBizCustomerArchiveExists(updateReqVO.getId());
        // 更新
        CustomerArchiveDO updateObj = BeanUtils.toBean(updateReqVO, CustomerArchiveDO.class);
        customerArchiveMapper.updateById(updateObj);
    }

    @Override
    public List<Map<String, Object>> getSimpleList(CustomerArchivePageReqVO pageReqVO) {
        return customerArchiveMapper.getSimpleList(pageReqVO);
    }
}